package com.cg;

import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;

@WebServlet("/student")
public class StudentServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String stuName="李四";//学生姓名
        String stuNo="100001";//学号
        int age=25;//年龄
        String major="软件工程";//专业

        Student stu = new Student();

        stu.setStuNo(stuNo);
        stu.setStuName(stuName);
        stu.setAge(age);
        stu.setMajor(major);
        //存到应用作用域
//        ServletContext context = getServletContext();
//        context.setAttribute("stuNo",stuNo);
//        context.setAttribute("stuName",stuName);
//        context.setAttribute("age",age);
//        context.setAttribute("major",major);

//        //获取到当前会话Session对象
//        HttpSession session=req.getSession();
//        //将学生信息存储到当前会话作用域,保存用户状态下信息时可以使用，如保存账号的信息
//        session.setAttribute("stuName",stuName);
//        session.setAttribute("stuNo",stuNo);
//        session.setAttribute("age",age);
//        session.setAttribute("major",major);
//        //跳转到学生jsp页面
//        resp.sendRedirect(req.getContextPath()+"/student.jsp");

        //请求作用域一般较多
//        req.setAttribute("stuName",stuName);
//        req.setAttribute("stuNo",stuNo);
//        req.setAttribute("age",age);
//        req.setAttribute("major",major);
        req.setAttribute("stu",stu);
        //通过请求转发跳转到JSP页面
        req.getRequestDispatcher("/student.jsp").forward(req,resp);
    }
}
